package com.supor.aiot.mine.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import com.android.baseconfig.common.utils.Util;
import com.supor.aiot.mine.R;

import androidx.annotation.Nullable;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2021, by het, ShenZhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: TriangleView <br>
 * 作者: lei <br>
 * 日期: 2021/8/18 <br>
 */
public class TriangleView extends View {
    private final static int TRIANGLE_DIRECTION_TOP = 1;
    private final static int TRIANGLE_DIRECTION_BOTTOM = 2;
    private final static int TRIANGLE_DIRECTION_LEFT = 1;
    private final static int TRIANGLE_DIRECTION_RIGHT = 2;

    private Paint mPaint;

    private int mBgColor;
    private int mTriangleHeight;
    private int mTriangleWidth;
    private int triangleDirection;

    public TriangleView(Context context) {
        this(context, null);
    }

    public TriangleView(Context context,
                        @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TriangleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TriangleView);
            mBgColor = a.getColor(R.styleable.TriangleView_bgColor, 0);
            mTriangleHeight =
                    a.getDimensionPixelOffset(R.styleable.TriangleView_triangleH,
                            Util.dip2px(context, 6));
            mTriangleWidth =
                    a.getDimensionPixelOffset(R.styleable.TriangleView_triangleW,
                            Util.dip2px(context, 6));
            triangleDirection = a.getInt(R.styleable.TriangleView_triangle_direction, 0);
            a.recycle();
        }

        initPaint();
    }

    //初始化画笔
    public void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setDither(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(mTriangleWidth, mTriangleHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mBgColor != 0) {
            mPaint.setColor(mBgColor);
            drawTriangle(canvas, mPaint);
        }
        super.onDraw(canvas);
    }

    //绘制三角形
    private void drawTriangle(Canvas canvas, Paint paint) {
        Path path = new Path();
        switch (triangleDirection) {
            //上
            case TRIANGLE_DIRECTION_TOP:
                // path.moveTo(mTriangleWidth / 2, mTriangleHeight);
                path.moveTo(0, mTriangleHeight);
                path.lineTo(mTriangleWidth / 2, 0);
                path.lineTo(mTriangleWidth, mTriangleHeight);
                break;
            //下
            case TRIANGLE_DIRECTION_BOTTOM:
                path.moveTo(0, 0);
                path.lineTo(mTriangleWidth / 2, mTriangleHeight);
                path.lineTo(mTriangleWidth, 0);
                break;
            default:
                return;
        }
        canvas.drawPath(path, paint);
    }

    public void reInit(int bgColor, int triangleWidth, int triangleHeight, int triangleDirection) {
        setBgColor(bgColor);
        setTriangleWidth(triangleWidth);
        setTriangleHeight(triangleHeight);
        setTriangleDirection(triangleDirection);
        invalidate();
    }

    public void setBgColor(int bgColor) {
        this.mBgColor = bgColor;
    }

    public void setTriangleDirection(int triangleDirection) {
        this.triangleDirection = triangleDirection;
    }

    public void setTriangleHeight(int triangleHeight) {
        this.mTriangleHeight = triangleHeight;
    }

    public void setTriangleWidth(int triangleWidth) {
        this.mTriangleWidth = triangleWidth;
    }
}
